package server.handler;

import java.awt.Point;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import server.command.ActionCommand;
import server.command.Command;

public class ActionCommandHandler extends CommandHandler {

	private Robot robot;

	public ActionCommandHandler(Robot robot) {
		this.robot = robot;
	}

	@Override
	public void handle(Command command) {
		switch (command.getType()) {
		case ActionCommand.ACTION_CHG_HILIGHT:
			chgHilight(command.getPosition());
			break;
		case ActionCommand.ACTION_CHG_PEN:
			chgPen(command.getPosition());
			break;
		case ActionCommand.ACTION_ERS_ALL:
			ersAll(command.getPosition());
			break;
		case ActionCommand.ACTION_ERS_PRE:
			ersPre(command.getPosition());
			break;
		case ActionCommand.ACTION_JMP:
			jmpSlide(command.getPosition());
			break;
		}
	}

	private void jmpSlide(Integer index) {
		String str = index.toString();
		for (int i = 0; i < str.length(); i++) {
			robot.keyPress(str.charAt(i));
			robot.keyRelease(str.charAt(i));
		}
		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
	}

	private void ersPre(int index) {
		System.out.println("eraser pre mark");
		Point point = MarkCommandHandler.getPrePoint(index);
		if (point == null)
			return;
		this.groupCtrl(KeyEvent.VK_E);
		robot.mouseMove((int) point.getX(), (int) point.getY());
		robot.mousePress(MouseEvent.BUTTON1_MASK);
		robot.mouseRelease(MouseEvent.BUTTON1_MASK);
	}

	private void ersAll(int index) {
		System.out.println("eraser all mark");
		robot.keyPress(KeyEvent.VK_E);
		robot.keyRelease(KeyEvent.VK_E);
	}

	private void chgHilight(int index) {
		System.out.println("change to highlight mode");
		this.groupCtrl(KeyEvent.VK_P);
	}

	private void chgPen(int index) {
		System.out.println("eraser to pen mode");
		this.groupCtrl(KeyEvent.VK_P);
	}

	// private void startCurrent() {
	// System.out.println("start from current slide");
	// this.groupShift(KeyEvent.VK_F5);
	// }
	//
	// private void startFirst() {
	// System.out.println("start from first slide");
	// robot.keyPress(KeyEvent.VK_F5);
	// robot.keyRelease(KeyEvent.VK_F5);
	// }

	private void groupCtrl(int key) {
		robot.keyPress(KeyEvent.VK_CONTROL);
		robot.keyPress(key);
		robot.keyRelease(key);
		robot.keyRelease(KeyEvent.VK_CONTROL);
	}

	// private void groupShift(int key) {
	// robot.keyPress(KeyEvent.VK_SHIFT);
	// robot.keyPress(key);
	// robot.keyRelease(key);
	// robot.keyRelease(KeyEvent.VK_SHIFT);
	// }
}
